package com.huawei.cloud.phone.management.domain.pkg;

import com.huawei.cloud.phone.management.application.pkg.dto.PkgDTO;
import com.huawei.cloud.phone.management.common.BeanFactory;
import com.huawei.cloud.phone.management.common.GlobalConfig;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Data
public class PkgData {
    private TarData tarData;

    private List<ObsFileData> apkData;

    /**
     * 转换成应用包数据
     *
     * @param pkgDTO 应用包传输数据
     * @return 应用包数据
     */
    public static PkgData convertPkgData(PkgDTO pkgDTO) {
        String pkgName = pkgDTO.getApk().getPkgName();
        List<ObsFileData> apkData = pkgDTO.getApk().getFiles().stream()
                .map(file -> ObsFileData.convertObsFileData(pkgName, file))
                .collect(Collectors.toList());
        PkgData pkgData = new PkgData();
        pkgData.setApkData(apkData);
        pkgData.setTarData(convertTarData(pkgName));
        return pkgData;
    }

    private static TarData convertTarData(String pkgName) {
        TarData tarData = new TarData();
        tarData.setPkgName(pkgName);
        tarData.setTarObjectPath("tar/" + pkgName + ".tar");
        tarData.setBucketName(BeanFactory.getBean(GlobalConfig.class).getBucketName());
        return tarData;
    }

    @Data
    public static class TarData {
        private String bucketName;

        private String pkgName;

        private String tarObjectPath;
    }
}
